package server.controller.goodController;

import server.entity.Code;
import server.model.factory.ModelFactory;
import server.util.InfoUtils.Info;
import server.util.InfoUtils.InfoEnum;
import server.util.InfoUtils.InfoUtil;
import server.util.MyUtils;
import server.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/purchase")
public class PurchaseServlet extends HttpServlet {

    private static final long serialVersionUID = -214676362930750339L;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Info info;
        Integer userId = (Integer) req.getAttribute("userId");

        String goodId = req.getParameter("goodId");
        //获取购买的数量
        String count = req.getParameter("count");

        if (!MyUtils.NOTNULL(goodId, count)) {  //判断字段是否为空
            info = InfoUtil.getInfo(InfoEnum.PARAMS_NULL);
        } else {
            info = ModelFactory.getGoodModel().purchase(userId, Integer.parseInt(goodId), Integer.parseInt(count));
        }

        ResponseUtil.responseMessage(resp, info);
    }
}
